﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.Model;
using Gift.WebApp;

namespace WebApp.admin.bill
{
    public partial class billProcess : BaseAdmin
    {
      
        tbBill oBill;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                this.BindTypeList();
                this.BindCompanyList();
                this.BuildForm();
            }
        }


        void BindTypeList()
        {
            DataSet ds = BaseFactory.theBill.GetTypeList("");
            this.ddlBillType.DataTextField = "name";
            this.ddlBillType.DataValueField = "id";
            this.ddlBillType.DataSource = ds;
            this.ddlBillType.DataBind();
        }

        void BindCompanyList()
        {
            DataSet ds = BaseFactory.theCompany.GetList("");
            this.ddlCompany.DataTextField = "name";
            this.ddlCompany.DataValueField = "id";
            this.ddlCompany.DataSource = ds;
            this.ddlCompany.DataBind();
            this.ddlCompany.Items.Insert(0, new ListItem("请选择开票公司名称...", ""));
        }

        void BuildForm()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                oBill = BaseFactory.theBill.GetModel(Request.QueryString["id"]);
                if (oBill != null)
                {
                    this.txtTitleName.Text = oBill.TitleName;
                    this.txtMoney.Text = oBill.Money.ToString();
                    this.txtRemark.Text = oBill.Remark.ToString();
                    this.ddlCompany.Items.FindByValue(oBill.CompanyId.ToString()).Selected = true;
                    this.rblClass.Items.FindByValue(oBill.ClassName).Selected = true;
                    this.ddlBillType.Items.FindByValue(oBill.BillTypeId.ToString()).Selected = true;
                    this.ddlStates.Items.FindByValue(oBill.States).Selected = true;
                    this.txtRemark.Text = oBill.Remark;
                    this.txtAddressTelphone.Text = oBill.AddressTel;
                    this.txtTaxer.Text = oBill.TaxId;
                    this.txtBankCode.Text = oBill.BankCode;

                    this.txtTitleName.Enabled = false;
                    this.txtMoney.Enabled = false;
                    this.ddlCompany.Enabled = false;
                    this.rblClass.Enabled = false;
                    this.txtRemark.Enabled = false;
                    this.txtAddressTelphone.Enabled = false;
                    this.ddlBillType.Enabled = false;
                    this.txtTaxer.Enabled = false;
                    this.txtBankCode.Enabled = false;

                    if (oBill.ClassName == "普通发票")
                    {
                        this.trAddressTel.Visible = false;
                        this.trTaxer.Visible = false;
                        this.trBankCode.Visible = false;
                    }
                    else
                    {
                        this.trAddressTel.Visible = true;
                        this.trTaxer.Visible = true;
                        this.trBankCode.Visible = true;
                    }
                }
            }
            else
            {
                if (this.rblClass.SelectedItem.Value == "普通发票")
                {
                    this.trAddressTel.Visible = false;
                    this.trTaxer.Visible = false;
                    this.trBankCode.Visible = false;
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            oBill = new tbBill();
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                oBill = BaseFactory.theBill.GetModel(Request.QueryString["id"]);

                oBill.ProcessBillTime = DateTime.Now;
                oBill.States = this.ddlStates.SelectedItem.Value;
                oBill.ProcessRemark = this.txtProcessRemark.Text.Trim();
                oBill.AdminId = int.Parse(base.Administrator.AdminUserId);
                BaseFactory.theBill.Update(oBill);

            }
            base.Response.Write("<script language='javascript'>alert('保存成功！');window.location.href='billList.aspx';</script>");
        }
    }
}